import { Component, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { file, fileInfoDto, folder } from 'ispace.core.main';
import {MatListModule} from '@angular/material/list';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-property-page',
  standalone: true,
  imports: [MatListModule,CommonModule],
  templateUrl: './property-page.component.html',
  styleUrl: './property-page.component.scss'
})
export class PropertyPageComponent {
formatFileSize(size: number|undefined) {
  if (size === undefined) {
    return "N/A";
  }
  const units = ["B", "KB", "MB", "GB", "TB"];
  let unitIndex = 0;
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024;
    unitIndex++;
  }
  return `${size.toFixed(2)} ${units[unitIndex]}`;
}

  constructor(private route: ActivatedRoute) { }

  @Input() fileInfo?: fileInfoDto;

  path = "";
  type = "";
  info?:fileInfoDto;

  str:string = "";

  ngOnInit(): void { 
 
    if(this.fileInfo) {
      this.info = this.fileInfo;
      this.str = JSON.stringify(this.info, null, 2);
      return;
    }

    // step 1: 获取url参数路径 
    this.route.queryParams.subscribe(async queryParams => {
      this.path = queryParams['path'];  
      this.type = queryParams['type'];
        this.info = await this.getInfo(); 
        this.str = JSON.stringify(this.info, null, 2);
  }); 

}

  async getInfo(){
      let data: fileInfoDto | undefined;
      if(this.type == 'file')  {
         data = await file.statf(this.path).toPromise();
      } else {
        data = await folder.statf(this.path).toPromise();
      } 
      return data; 
  }

}
